// pages/template/mall.js
var app = getApp();
var goodslist=[];
Page({

  /**
   * 页面的初始数据
   */
  data: {
    config: {},
    goods: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // wx.navigateTo({
    //   url: '/bookOffline/pages/index',
    // })
    // return;
    var extConfigSync = app.getExtConfigSync();
    var that = this;
    var shop_info = app.getShopInfo();
    // console.log(shop_info.name);
    wx.setNavigationBarTitle({
      title: shop_info.name,
    })
    this.setData({ theme: extConfigSync.theme, shop_info: shop_info});
    this.getPageConfig();
  },

  //页面配置
  getPageConfig: function () {
    app.api("mall/pages/getPageConfig", {
      page: "pages/index/index",
      shop_id: this.data.shop_info.id
    }, (res) => {
      if (res.code != 200) {
        app.alert(res.msg);
      }
      this.setData({ config: res.data });
      this.loadList();
    })
  },
  //商品列表
  loadList: function () {
    var that = this;

    var config = this.data.config;
    if (!config.attrs) {
      that.setData({
        goods: []
      });
      return;
    }

    this.setData({ config: config });

    for (let i in config.attrs) {
      var params = config.attrs[i].params;
      app.api("mall/goods/get", params, (res) => {
        if (res.code != 200) {
          app.alert(res.msg);
        }

        if (res.data && res.data.list) {
          goodslist[i] = res.data.list;
        } else {
          goodslist[i] = [];
        }
        var goods = []
        for (var k in goodslist[i]) {
          var item = goodslist[i][k];
          goods.push({
            default_img: item['default_img'],
            goods_name: item['goods_name'],
            origin_price: item['origin_price'],
            price: item['price'],
            id: item['id'],
            total_stock: item['total_stock'],
            sub_title: item['sub_title'],
          })
        }
        that.data.goods[i] = goods;
        that.setData({
          goods: that.data.goods
        });

      })
    }


  },
  //浏览图片
  previewListImage: function (e) {
    var url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: url
    })
    return;
    var current = e.target.dataset.src;
    var imglist = this.data.config.swiper_params;
    var pics = [];
    for (var i in imglist) {
      pics.push(imglist[i].img);
    }
    wx.previewImage({
      current: current,
      urls: pics
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getPageConfig();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})